#include "missionWidget.h"

MissionWidget::MissionWidget(QWidget* p):QWidget(p),ui(new Ui::MissionWidget),filePath(""),workPath("")
{
    ui->setupUi(this);
    setInputMask();

    connect(ui->treeWidget_mission_item,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(s_rewardItemDoubleClicked(QTreeWidgetItem*,int)));
    connect(ui->tableWidget_mission_item,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(s_cellDoubleClicked(int,int)));
}

MissionWidget::~MissionWidget()
{
    delete ui;
}

void MissionWidget::setInputMask()
{
    QIntValidator* charValidator = new QIntValidator(-128,127,this);
    ui->lineEdit_mission_completeness->setValidator(charValidator);
    ui->lineEdit_mission_animation->setValidator(charValidator);
}

void MissionWidget::setFilePath(QString s)
{
    filePath = s;
    QStringList strList = filePath.split("/");
    ui->lineEdit_mission_cityName->setText(strList[strList.length()-3]);
    ui->lineEdit_mission_eventName->setText(strList[strList.length()-2]);
}

Mission MissionWidget::getMissionFromUI()
{
    Mission m;

    m.id = (qint32)ui->lineEdit_mission_id->text().toInt();
    m.name = ui->lineEdit_mission_name->text();
    m.description = ui->plainTextEdit_mission_description->toPlainText();
    m.energy = (qint32)ui->lineEdit_mission_energy->text().toInt();
    m.completeness = (qint8)ui->lineEdit_mission_completeness->text().toInt();
    m.reward_exp = (qint32)ui->lineEdit_mission_exp->text().toInt();
    m.reward_cash = (qint32)ui->lineEdit_mission_cash->text().toInt();

    int cnt = 0;
    while(ui->tableWidget_mission_item->item(cnt,0))
    {
        if(ui->tableWidget_mission_item->item(cnt,0)->text().replace(" ","").isEmpty())
            break;
        cnt++;
    }
    m.random_reward_item_count = (qint32)cnt;
    m.random_reward_item = new RewardItem[cnt];
    for(int i=0;i<cnt;i++)
    {
        m.random_reward_item[i].id = (qint32) ui->tableWidget_mission_item->item(i,2)->text().toInt();
        m.random_reward_item[i].possibility = (qint8) ui->tableWidget_mission_item->item(i,1)->text().toInt();
        m.random_reward_item[i].type =(qint8) ui->tableWidget_mission_item->item(i,3)->text().toInt();
    }

    m.buttonStr = ui->lineEdit_mission_btnStr->text();
    m.animation = (qint8) ui->lineEdit_mission_animation->text().toInt();

    return m;
}

void MissionWidget::displayMissionToUI(Mission& m)
{
    clearUI();

    QStringList strList = filePath.split("/");
    ui->lineEdit_mission_cityName->setText(strList[strList.length()-3]);
    ui->lineEdit_mission_eventName->setText(strList[strList.length()-2]);

    ui->lineEdit_mission_id->setText(QString::number(m.id));
    ui->lineEdit_mission_name->setText(m.name);
    ui->plainTextEdit_mission_description->setPlainText(m.description);
    ui->lineEdit_mission_energy->setText(QString::number(m.energy));
    ui->lineEdit_mission_completeness->setText(QString::number(m.completeness));
    ui->lineEdit_mission_exp->setText(QString::number(m.reward_exp));
    ui->lineEdit_mission_cash->setText(QString::number(m.reward_cash));

    for(int i=0;i<m.random_reward_item_count;i++)
    {
        switch(m.random_reward_item[i].type)
        {
        case EQUIP_IDX:
            ui->tableWidget_mission_item->setItem(i,0,new QTableWidgetItem(equipMap.value((int)m.random_reward_item[i].id)));
            break;
        case GENERAL_IDX:
            ui->tableWidget_mission_item->setItem(i,0,new QTableWidgetItem(generalMap.value((int)m.random_reward_item[i].id)));
            break;
        case FRAGMENT_IDX:
            ui->tableWidget_mission_item->setItem(i,0,new QTableWidgetItem(fragmentMap.value((int)m.random_reward_item[i].id)));
            break;
        }

        ui->tableWidget_mission_item->setItem(i,3,new QTableWidgetItem(QString::number(m.random_reward_item[i].type)));
        ui->tableWidget_mission_item->setItem(i,2,new QTableWidgetItem(QString::number(m.random_reward_item[i].id)));
        ui->tableWidget_mission_item->setItem(i,1,new QTableWidgetItem(QString::number(m.random_reward_item[i].possibility)));
    }
    ui->lineEdit_mission_btnStr->setText(m.buttonStr);
    ui->lineEdit_mission_animation->setText(QString::number(m.animation));
}

Mission MissionWidget::readMissionFromFile()
{
    Mission m;
    QFile f(filePath);
    if(f.exists())
    {
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>m;
        f.close();
        displayMissionToUI(m);
    }else
        WARNING_FILE_NOT_EXIST;

    return m;
}

bool MissionWidget::saveMissionToFile()
{
    if(ui->lineEdit_mission_name->text().isEmpty()||filePath.isEmpty())
    {
        WARNING_NO_NAME;
        return false;
    }
    else
    {
        Mission m = getMissionFromUI();
        QFile f(filePath);
        f.open(QIODevice::WriteOnly);
        QDataStream out(&f);
        out<<m;
        f.close();

        int idx1 = filePath.lastIndexOf("/");
        int idx2 = filePath.lastIndexOf(".");

        filePath.replace(idx1+1,idx2-idx1-1,ui->lineEdit_mission_name->text());
        f.rename(filePath);
        return true;
    }
}


void MissionWidget::exportToBinary(QString workspacepath)
{
    QMap<int,QString> cityMap,eventMap;
    cityMap = Utils::readMap(workspacepath+"/"+CITY_EXPORT_NAME);
    eventMap = Utils::readMap(workspacepath+"/"+EVENT_EXPORT_NAME);

    //export mission
    QFile f1(workspacepath+"/"+MISSION_EXPORT_NAME);
    f1.open(QIODevice::WriteOnly);
    QDataStream out1(&f1);

    out1<<(qint32)Mission::magic;
    out1<<(qint32)Mission::version;
    QDir dir_city(workspacepath+"/"+MISSION_STR);
//    QStringList cityList = dir_city.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
    QStringList cityList = Utils::getSeq(dir_city.absolutePath()+"/seq");
    int cnt_city = cityList.length();
    out1<<(qint32)cnt_city;
    for(int i=0;i<cnt_city;i++)
    {
        out1<<(qint32)cityMap.key(cityList.at(i));
        out1<<cityList.at(i).toUtf8().data();
//        qDebug()<<cityList.at(i);
        QDir dir_event(dir_city.absolutePath()+"/"+cityList.at(i));
//        QStringList eventList = dir_event.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
        QStringList eventList = Utils::getSeq(dir_event.absolutePath()+"/seq");
        int cnt_event = eventList.length();
        out1<<(qint32)cnt_event;
        for(int j=0;j<cnt_event;j++)
        {
//            qDebug()<<"     "<<eventList.at(j);
            out1<<(qint32)eventMap.key(eventList.at(j));
            out1<<eventList.at(j).toUtf8().data();
            QDir dir_mission(dir_event.absolutePath()+"/"+eventList.at(j));
//            QStringList missionList = dir_mission.entryList(QStringList(QString("*")+MISSION_FILE_SUFFIX));
            QStringList missionList = Utils::getSeq(dir_mission.absolutePath()+"/seq");
            int cnt_mission = missionList.length();
            out1<<(qint32)cnt_mission;
            for(int k=0;k<cnt_mission;k++)
            {
//                qDebug()<<"            "<<missionList.at(k);
                Mission m;
                QFile mf(dir_mission.absolutePath()+"/"+missionList.at(k));
                mf.open(QIODevice::ReadOnly);
                QDataStream in(&mf);
                in>>m;
                mf.close();
                out1<<m;
            }
        }
    }

    f1.close();
}

void MissionWidget::exportToXML(QString workspacepath)
{
    //read CityMap
    QMap<int,QString> cityMap,eventMap;
    cityMap = Utils::readMap(workspacepath+"/"+CITY_EXPORT_NAME);
    eventMap = Utils::readMap(workspacepath+"/"+EVENT_EXPORT_NAME);

    //*****write XML*******//
    QFile file(workspacepath+"/output/mission.xml");
    file.open(QIODevice::WriteOnly);
    QDomDocument doc;
    QDomText text;
    QDomProcessingInstruction instruction;
    instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"gb18030\"");
    doc.appendChild(instruction);
    QDomElement root=doc.createElement("root");
    doc.appendChild(root);

    QDir dir_city(workspacepath+"/"+MISSION_STR);
    QStringList cityList = Utils::getSeq(dir_city.absolutePath()+"/seq");//dir_city.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
    int cnt_city = cityList.length();
    for(int i=0;i<cnt_city;i++)
    {
        QDomElement city=doc.createElement("city");
        root.appendChild(city);

        QDomElement id_city=doc.createElement("id");
        text=doc.createTextNode(QString::number(cityMap.key(cityList.at(i))));
        id_city.appendChild(text);
        QDomElement name_city=doc.createElement("name");
        text=doc.createTextNode(cityList.at(i));
        name_city.appendChild(text);
        city.appendChild(id_city);
        city.appendChild(name_city);

        QDir dir_event(dir_city.absolutePath()+"/"+cityList.at(i));
        QStringList eventList = Utils::getSeq(dir_event.absolutePath()+"/seq");//dir_event.entryList(QDir::Dirs|QDir::NoDotAndDotDot);
        int cnt_event = eventList.length();
        for(int j=0;j<cnt_event;j++)
        {
            QDomElement event=doc.createElement("event");
            city.appendChild(event);

            QDomElement id_event=doc.createElement("id");
            text=doc.createTextNode(QString::number(eventMap.key(eventList.at(j))));
            id_event.appendChild(text);
            QDomElement name_event=doc.createElement("name");
            text=doc.createTextNode(eventList.at(j));
            name_event.appendChild(text);
            event.appendChild(id_event);
            event.appendChild(name_event);

            QDir dir_mission(dir_event.absolutePath()+"/"+eventList.at(j));
            QStringList missionList = Utils::getSeq(dir_mission.absolutePath()+"/seq");//dir_mission.entryList(QStringList(QString("*")+MISSION_FILE_SUFFIX));
            int cnt_mission = missionList.length();

            for(int k=0;k<cnt_mission;k++)
            {
                QDomElement mission=doc.createElement("mission");
                event.appendChild(mission);

                Mission m;
                QFile mf(dir_mission.absolutePath()+"/"+missionList.at(k));
                mf.open(QIODevice::ReadOnly);
                QDataStream in(&mf);
                in>>m;
                mf.close();

                QDomElement missionElement;

                missionElement=doc.createElement("id");
                text=doc.createTextNode(QString::number(m.id));
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("name");
                text=doc.createTextNode(m.name);
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("description");
                text=doc.createTextNode(m.description);
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("energy");
                text=doc.createTextNode(QString::number(m.energy));
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("difficulty");
                text=doc.createTextNode(QString::number(m.difficulty));
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("completeness");
                text=doc.createTextNode(QString::number(m.completeness));
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("reward_exp");
                text=doc.createTextNode(QString::number(m.reward_exp));
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("reward_cash");
                text=doc.createTextNode(QString::number(m.reward_cash));
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("buttonStr");
                text=doc.createTextNode(m.buttonStr);
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                missionElement=doc.createElement("animationID");
                text=doc.createTextNode(QString::number(m.animation));
                missionElement.appendChild(text);
                mission.appendChild(missionElement);

                for(int l=0;l<m.random_reward_item_count;l++)
                {
                    QDomElement rewardElement=doc.createElement("random_reward_item");
                    mission.appendChild(rewardElement);

                    missionElement=doc.createElement("id");
                    text=doc.createTextNode(QString::number(m.random_reward_item[l].id));
                    missionElement.appendChild(text);
                    rewardElement.appendChild(missionElement);

                    missionElement=doc.createElement("possibility");
                    text=doc.createTextNode(QString::number(m.random_reward_item[l].possibility));
                    missionElement.appendChild(text);
                    rewardElement.appendChild(missionElement);

                    missionElement=doc.createElement("type");
                    text=doc.createTextNode(QString::number(m.random_reward_item[l].type));
                    missionElement.appendChild(text);
                    rewardElement.appendChild(missionElement);
                }
            }
        }
    }

    QTextStream out(&file);
    doc.save(out,4);
    file.close();
}

bool MissionWidget::importFromXML(QString workspacepath, QString xmlFile)
{
    QFile f(xmlFile);
    f.open(QIODevice::ReadOnly|QIODevice::Text);

    QDomDocument docment;

    if(!docment.setContent(&f,false))
    {
        return false;
    }

    QMap<int,QString> cMap,eMap;

    QDir dir_root(workspacepath+"/"+MISSION_STR);
    QDomElement root = docment.documentElement();

    QDomNodeList cityChildren = root.childNodes();
    if((cityChildren.length()==0)||(root.elementsByTagName("city").length()==0))
    {
        return false;
    }
    QStringList cityList;
    for(int i=0;i<cityChildren.length();i++)
    {
        QDomNodeList eventChildren = cityChildren.at(i).toElement().childNodes();
        QString str_city_id = eventChildren.at(0).toElement().text();
        QString str_city_name = eventChildren.at(1).toElement().text();
        cityList.push_back(str_city_name);
        cMap.insert(str_city_id.toInt(),str_city_name);

        dir_root.mkdir(str_city_name);
        QDir dir_city(dir_root.absolutePath()+"/"+str_city_name);
        QStringList eventList;
        for(int j=2;j<eventChildren.length();j++)
        {
            QDomNodeList missionChildren = eventChildren.at(j).toElement().childNodes();
            QString str_event_id = missionChildren.at(0).toElement().text();
            QString str_event_name = missionChildren.at(1).toElement().text();
            eventList.push_back(str_event_name);
            eMap.insert(str_event_id.toInt(),str_event_name);

            dir_city.mkdir(str_event_name);
            QDir dir_event(dir_city.absolutePath()+"/"+str_event_name);
            QStringList missionList;
            for(int k=2;k<missionChildren.length();k++)
            {
                Mission m;
                QDomNodeList missionAtr = missionChildren.at(k).toElement().childNodes();

                m.id = (qint32) missionAtr.at(0).toElement().text().toInt();
                m.name = missionAtr.at(1).toElement().text();
                m.description = missionAtr.at(2).toElement().text();
                m.energy = (qint32) missionAtr.at(3).toElement().text().toInt();
                m.difficulty = (qint8) missionAtr.at(4).toElement().text().toInt();
                m.completeness = (qint8) missionAtr.at(5).toElement().text().toInt();
                m.reward_exp = (qint32) missionAtr.at(6).toElement().text().toInt();
                m.reward_cash = (qint32) missionAtr.at(7).toElement().text().toInt();
                m.buttonStr = missionAtr.at(8).toElement().text();
                m.animation = (qint8) missionAtr.at(9).toElement().text().toInt();

                QDomNodeList rewardList = missionChildren.at(k).toElement().elementsByTagName("random_reward_item");
                m.random_reward_item_count = (qint32) rewardList.length();
                if(rewardList.length()>0)
                {
                    m.random_reward_item = new RewardItem[rewardList.length()];
                    for(int l=0;l<rewardList.length();l++)
                    {
                        QDomNodeList rewardAtr = rewardList.at(l).toElement().childNodes();
                        m.random_reward_item[l].id = (qint32) rewardAtr.at(0).toElement().text().toInt();
                        m.random_reward_item[l].possibility = (qint8) rewardAtr.at(1).toElement().text().toInt();
                        m.random_reward_item[l].type = (qint8) rewardAtr.at(2).toElement().text().toInt();
                    }
                }

                QString missionName = m.name+QString(MISSION_FILE_SUFFIX);

                QFile mf(dir_event.absolutePath()+"/"+missionName);
                mf.open(QIODevice::WriteOnly);
                QDataStream out(&mf);
                out<<m;
                mf.close();

                missionList.push_back(missionName);
            }
            Utils::writeSeq(dir_event.absolutePath()+"/seq",missionList);
        }
        Utils::writeSeq(dir_city.absolutePath()+"/seq",eventList);
    }
    Utils::writeSeq(dir_root.absolutePath()+"/seq",cityList);
    Utils::writeMap(workspacepath+"/"+CITY_EXPORT_NAME,cMap);
    Utils::writeMap(workspacepath+"/"+EVENT_EXPORT_NAME,eMap);

    return true;
}


void MissionWidget::clearUI()
{
    initialMap();
    ui->lineEdit_mission_id->clear();
    ui->lineEdit_mission_name->clear();
    ui->lineEdit_mission_energy->clear();
    ui->lineEdit_mission_animation->clear();
    ui->lineEdit_mission_btnStr->clear();
    ui->lineEdit_mission_cash->clear();
    ui->lineEdit_mission_cityName->clear();
    ui->lineEdit_mission_completeness->clear();
    ui->lineEdit_mission_eventName->clear();
    ui->lineEdit_mission_exp->clear();
    ui->plainTextEdit_mission_description->clear();
    for(int j=0;j<ui->tableWidget_mission_item->rowCount();j++)
    {
        for(int i=0;i<ui->tableWidget_mission_item->columnCount();i++)
        {
            if(ui->tableWidget_mission_item->item(j,i))
                delete (ui->tableWidget_mission_item->item(j,i));
        }
    }
}

void MissionWidget::initialMap()
{
    equipMap.clear();
    generalMap.clear();
    fragmentMap.clear();

    //initial equipMap
    QDir dir_weapon(workPath+"/"+EQUIP_STR+"/"+WEAPON_STR);
    QDir dir_armor(workPath+"/"+EQUIP_STR+"/"+ARMOR_STR);
    QDir dir_horse(workPath+"/"+EQUIP_STR+"/"+HORSE_STR);

    QString mfilter = QString("*")+QString(EQUIP_FILE_SUFFIX);
    QStringList weaponList = dir_weapon.entryList(QStringList()<<mfilter);
    QStringList armorList = dir_armor.entryList(QStringList()<<mfilter);
    QStringList horseList = dir_horse.entryList(QStringList()<<mfilter);

    Equipment e;

    for(int i=0;i<weaponList.length();i++)
    {
        QFile f(dir_weapon.absolutePath()+"/"+weaponList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>e;
        equipMap.insert((int)e.id,e.name);
        f.close();
    }

    for(int i=0;i<armorList.length();i++)
    {
        QFile f(dir_armor.absolutePath()+"/"+armorList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>e;
        equipMap.insert((int)e.id,e.name);
        f.close();
    }

    for(int i=0;i<horseList.length();i++)
    {
        QFile f(dir_horse.absolutePath()+"/"+horseList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>e;
        equipMap.insert((int)e.id,e.name);
        f.close();
    }

    //initial generalMap
    QDir dir_five(workPath+"/"+GENERAL_STR+"/"+GENERAL_FIVE_STAR_STR);
    QDir dir_four(workPath+"/"+GENERAL_STR+"/"+GENERAL_FOUR_STAR_STR);
    QDir dir_three(workPath+"/"+GENERAL_STR+"/"+GENERAL_THREE_STAR_STR);
    QDir dir_two(workPath+"/"+GENERAL_STR+"/"+GENERAL_TWO_STAR_STR);
    QDir dir_one(workPath+"/"+GENERAL_STR+"/"+GENERAL_ONE_STAR_STR);

    mfilter = QString("*")+QString(GENERAL_FILE_SUFFIX);
    QStringList fiveList = dir_five.entryList(QStringList()<<mfilter);
    QStringList fourList = dir_four.entryList(QStringList()<<mfilter);
    QStringList threeList = dir_three.entryList(QStringList()<<mfilter);
    QStringList twoList = dir_two.entryList(QStringList()<<mfilter);
    QStringList oneList = dir_one.entryList(QStringList()<<mfilter);

    General g;

    for(int i=0;i<fiveList.length();i++)
    {
        QFile f(dir_five.absolutePath()+"/"+fiveList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>g;
        generalMap.insert((int)g.id,g.name);
        f.close();
    }

    for(int i=0;i<fourList.length();i++)
    {
        QFile f(dir_four.absolutePath()+"/"+fourList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>g;
        generalMap.insert((int)g.id,g.name);
        f.close();
    }

    for(int i=0;i<threeList.length();i++)
    {
        QFile f(dir_three.absolutePath()+"/"+threeList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>g;
        generalMap.insert((int)g.id,g.name);
        f.close();
    }

    for(int i=0;i<twoList.length();i++)
    {
        QFile f(dir_two.absolutePath()+"/"+twoList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>g;
        generalMap.insert((int)g.id,g.name);
        f.close();
    }

    for(int i=0;i<oneList.length();i++)
    {
        QFile f(dir_one.absolutePath()+"/"+oneList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>g;
        generalMap.insert((int)g.id,g.name);
        f.close();
    }

    //initial fragmentMap
    QDir dir_fragment(workPath+"/"+FRAGMENT_STR);
    mfilter =  QString("*")+QString(FRAGMENT_FILE_SUFFIX);
    QStringList fragmentList = dir_fragment.entryList(QStringList()<<mfilter);
    Fragment fragment;
    for(int i=0;i<fragmentList.length();i++)
    {
        QFile f(dir_fragment.absolutePath()+"/"+fragmentList.at(i));
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>fragment;
        fragmentMap.insert((int)fragment.id,fragment.name);
        f.close();
    }
}

void MissionWidget::addTreeItems(QList<QTreeWidgetItem *> items)
{

    ui->treeWidget_mission_item->clear();

    int cnt = items.length();
    for(int i=0;i<cnt;i++)
    {
        QTreeWidgetItem* tmp2 = items.at(i);
        ui->treeWidget_mission_item->invisibleRootItem()->addChild(tmp2->clone());
    }
}

//*********slot function***************//
void MissionWidget::s_rewardItemDoubleClicked(QTreeWidgetItem* item,int col)
{
    int cnt = 0;
    while(ui->tableWidget_mission_item->item(cnt,0))
    {
        if(ui->tableWidget_mission_item->item(cnt,0)->text().replace(" ","").isEmpty())
            break;
        cnt++;
    }

    QString itemText = item->text(col);

    if(itemText.endsWith(QString(EQUIP_FILE_SUFFIX)))
    {
        QString name = item->text(col).replace(QString(EQUIP_FILE_SUFFIX),"");
        ui->tableWidget_mission_item->setItem(cnt,0,new QTableWidgetItem(name));
        ui->tableWidget_mission_item->setItem(cnt,3,new QTableWidgetItem(QString::number(EQUIP_IDX)));

        ui->tableWidget_mission_item->setItem(cnt,2,new QTableWidgetItem(QString::number((int)equipMap.key(name))));
    }
    if(itemText.endsWith(QString(GENERAL_FILE_SUFFIX)))
    {
        QString name = item->text(col).replace(QString(GENERAL_FILE_SUFFIX),"");
        ui->tableWidget_mission_item->setItem(cnt,0,new QTableWidgetItem(name));
        ui->tableWidget_mission_item->setItem(cnt,3,new QTableWidgetItem(QString::number(GENERAL_IDX)));

        ui->tableWidget_mission_item->setItem(cnt,2,new QTableWidgetItem(QString::number((int)generalMap.key(name))));
    }
    if(itemText.endsWith(QString(FRAGMENT_FILE_SUFFIX)))
    {
        QString name = item->text(col).replace(QString(FRAGMENT_FILE_SUFFIX),"");
        ui->tableWidget_mission_item->setItem(cnt,0,new QTableWidgetItem(name));
        ui->tableWidget_mission_item->setItem(cnt,3,new QTableWidgetItem(QString::number(FRAGMENT_IDX)));

        ui->tableWidget_mission_item->setItem(cnt,2,new QTableWidgetItem(QString::number((int)fragmentMap.key(name))));
    }
}

void MissionWidget::s_cellDoubleClicked(int row, int column)
{
    if(column==0||column==2||column==3)
    {
        delete (ui->tableWidget_mission_item->takeItem(row,0));
        delete (ui->tableWidget_mission_item->takeItem(row,1));
        delete (ui->tableWidget_mission_item->takeItem(row,2));
        delete (ui->tableWidget_mission_item->takeItem(row,3));
    }
}
